//
//  YHY_TravelNotes_VC.m
//  Travels
//
//  Created by yanghuiyan on 16/3/8.
//  Copyright © 2016年 yanghuiyan. All rights reserved.
//

#import "YHY_TravelNotes_VC.h"
#import "YHY_TravelNotesTableViewCell.h"
#import "YHY_TravelDetail_VC.h"
#import "YHYAFNetTool.h"
#import "TravelNotesModel.h"
#import "UserModel.h"
#import "MJRefresh.h"
#import "MBProgressHUD.h"
@interface YHY_TravelNotes_VC ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) UITableView *travelTableView;

@property (nonatomic, retain) NSMutableArray *dataSource;

@property (nonatomic, assign) NSInteger pages;

@property (nonatomic, retain) MBProgressHUD *HUD;

@end

@implementation YHY_TravelNotes_VC

- (void)dealloc {
    _travelTableView.dataSource = nil;
    _travelTableView.delegate = nil;
    
    [_travelTableView release];
    [_dataSource release];
    [super dealloc];
}

- (void)viewDidLoad {
    
    [super viewDidLoad];
   
//    self.navigationController.navigationBar.translucent = YES;
     self.dataSource = [NSMutableArray arrayWithCapacity:0];
    [self creatView];
    [self addFooter];
}

- (void)creatView {
    
    //自定义标题视图
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 40)];
    titleLabel.text = @"游  记";
    titleLabel.font = [UIFont boldSystemFontOfSize:25];
    titleLabel.textColor = [UIColor whiteColor];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    self.navigationItem.titleView = titleLabel;
    [titleLabel release];
    
    self.travelTableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    [self.travelTableView registerClass:[YHY_TravelNotesTableViewCell class] forCellReuseIdentifier:@"TravelNotesTableViewCellIdentifier"];
    self.travelTableView.delegate = self;
    self.travelTableView.dataSource = self;
    [self.view addSubview:self.travelTableView];
    
    [_travelTableView release];
    
}


- (void)addFooter {
    self.pages = 1;
    [self.travelTableView addFooterWithTarget:self action:@selector(dataAnalysis)];
    self.travelTableView.footerPullToRefreshText = @"加载更多";
    self.travelTableView.footerReleaseToRefreshText = @"松开加载";
    self.travelTableView.footerRefreshingText = @"正在加载";
    [self.travelTableView footerBeginRefreshing];
}

#pragma mark - UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
 
    return self.dataSource.count;
   
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    YHY_TravelNotesTableViewCell *cell = [self.travelTableView dequeueReusableCellWithIdentifier:@"TravelNotesTableViewCellIdentifier"];
    cell.travelModel = self.dataSource[indexPath.row];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 240 * [UIScreen mainScreen].bounds.size.height / 736;
}

#pragma mark - 点击跳转详情页
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    YHY_TravelDetail_VC *VC = [[YHY_TravelDetail_VC alloc] init];
    TravelNotesModel *model = self.dataSource[indexPath.row];
    VC.idNew = model.idNew;
    [self.navigationController pushViewController:VC animated:YES];
    self.navigationController.tabBarController.tabBar.hidden = YES;
    
    [VC release];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - 数据解析

- (void)dataAnalysis {
    
    [self loadPic];
    

    NSString *string = [NSString stringWithFormat:@"http://chanyouji.com/api/trips/featured.json?page=%ld", self.pages];
    [YHYAFNetTool GETNetWirhUrl:string body:nil headerFile:nil response:YHYJSON Success:^(id result) {
        
        
        for (NSDictionary *dic in result) {
            TravelNotesModel *model = [[TravelNotesModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.dataSource addObject:model];

        }
        
            [self.travelTableView reloadData];
            [self.travelTableView footerEndRefreshing];
            self.pages +=1;
        
       
        self.HUD.hidden = YES;
        
    
    } Failure:^(NSError *error) {
        NSLog(@"fail");
    
    }];
    
}

#pragma mark - 数据加载图标
- (void)loadPic {
    
    //初始化
    self.HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    
    //设置文字
    self.HUD.labelText = @"努力加载中";
    
    //出现时间
    [self.HUD hide:YES afterDelay:5];
    
    //设置文字颜色
    self.HUD.labelColor = [UIColor whiteColor];
    
    //设置整体图标的颜色
    self.HUD.color = [UIColor grayColor];
}

//-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
//    
//    CATransform3D rotation;
//    rotation = CATransform3DMakeRotation( (90.0*M_PI)/180, 0.0, 0.7, 0.4);
//    rotation.m34 = 1.0/ -600;
//    
//    cell.layer.shadowColor = [[UIColor blackColor]CGColor];
//    cell.layer.shadowOffset = CGSizeMake(10, 10);
//    cell.alpha = 0;
//    cell.layer.transform = rotation;
//    cell.layer.anchorPoint = CGPointMake(0, 0.5);
//    
//    
//    [UIView beginAnimations:@"rotation" context:NULL];
//    [UIView setAnimationDuration:0.8];
//    cell.layer.transform = CATransform3DIdentity;
//    cell.alpha = 1;
//    cell.layer.shadowOffset = CGSizeMake(0, 0);
//    [UIView commitAnimations];
//}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
